package com.ebt.m.proposal_v2.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.proposal_v2.dao.request.ParamGetRisks;
import com.ebt.m.proposal_v2.dao.response.LabelInfo;
import com.ebt.m.proposal_v2.dao.response.ResponseMain;
import com.ebt.m.proposal_v2.mvp.presenter.MainItemViewPresenter;
import com.ebt.m.proposal_v2.utils.DateUtils;
import com.ebt.m.proposal_v2.utils.ViewUtils;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.commons.buscomponent.listview.IView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 主险产品ItemView
 *
 * @author archer.qi
 *         Created on 2017/6/2.
 */
public class MainProductItemView extends IView {

    @BindView(R.id.item_main_product_name)
    TextView mName;
    @BindView(R.id.item_main_product_brand)
    TextView mBrandName;
    @BindView(R.id.item_main_product_state)
    ImageView mState;
    @BindView(R.id.item_main_product_labelContainer)
    LinearLayout mLabelContainer;

    private MainItemViewPresenter mPresenter;
    private ResponseMain itemData;
    private ParamGetRisks param;

    public boolean isLabelMode = true; // 是否是标签模式

    public int PADDING_HORIZONTAL;
    public int PADDING_VERTICAL;
    public final int LEFT_MARGIN = (int) (10 * ViewUtils.screenDensity(getContext()));

    public MainProductItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }

    public MainProductItemView(Context context, ParamGetRisks param) {
        super(context);
        this.param = param;
        initView(context);
    }

    public MainProductItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        View contentView = inflate(context, R.layout.adapter_main_product, this);
        ButterKnife.bind(contentView, this);
        setOnClickListener(new ItemClick());
        mPresenter = new MainItemViewPresenter(context);

        // 标签参数
        PADDING_HORIZONTAL = (int) (6 * ViewUtils.screenDensity(getContext()));
        PADDING_VERTICAL = (int) (3 * ViewUtils.screenDensity(getContext()));
    }

    @Override
    public void update(Object... args) {
        itemData = (ResponseMain) args[0];
        if (itemData == null) {
            return;
        }
        setName(itemData.name);
        setBrandName(itemData.brandShortName);
        setEnabled(itemData.isEnable == 1);
        initLabelMode();
        addLabelItems(itemData.tagInfo);
    }

    public void setName(String name) {
        mName.setText(name == null ? "" : name);
    }

    public void setBrandName(String name) {
        mBrandName.setText(name == null ? "" : name);
    }

    public void setEnabled(boolean enabled) {
        mName.setEnabled(enabled);
    }

    public void setChecked(boolean checked) {
        mState.setVisibility(checked ? View.VISIBLE : View.GONE);
    }

    public void initLabelMode() {
        if (isLabelMode) {
            mLabelContainer.setVisibility(View.VISIBLE);
        } else {
            mLabelContainer.setVisibility(View.GONE);
        }
    }

    public void addLabelItems(List<LabelInfo> labels) {
        if (!isLabelMode || mLabelContainer.getVisibility() == View.GONE) {
            return;
        }
        if (mLabelContainer.getChildCount() > 0) {
            mLabelContainer.removeAllViews();
        }
        if (labels == null || labels.size() == 0) {
            return;
        }
        for (LabelInfo label : labels) {
            if (label == null || (label != null && TextUtils.isEmpty(label.getTagName()))) {
                continue;
            }
            if (!DateUtils.isBetween(label.getTagStartTime(), label.getTagEndTime())) {
                continue;
            }
            TextView mLabelView = createLabelItemView(label.getTagName(),
                    Color.WHITE, ViewUtils.parseColor(label.getBackColor(), Color.GRAY));
            mLabelContainer.addView(mLabelView);
        }
    }

    private TextView createLabelItemView(String text, int solidColor, int strokeColor) {
        LinearLayout.LayoutParams PARAM = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        if (mLabelContainer.getChildCount() == 0) {
            PARAM.leftMargin = 0;
        } else {
            PARAM.leftMargin = LEFT_MARGIN;
        }
        TextView labelView = new TextView(getContext());
        labelView.setPadding(PADDING_HORIZONTAL, PADDING_VERTICAL, PADDING_HORIZONTAL, PADDING_VERTICAL);
        labelView.setGravity(Gravity.CENTER);
        ViewUtils.setBackgroundDrawable(labelView, ViewUtils.createCorneredStrokedDrawable(4, solidColor, strokeColor));
        labelView.setLayoutParams(PARAM);
        labelView.setText(text);
        labelView.setTextColor(strokeColor);
        labelView.setTextSize(10);
        return labelView;
    }

    public class ItemClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if (itemData == null) {
                return;
            }
            if (itemData.isEnable == 0 && !TextUtils.isEmpty(itemData.warnMsg)) {
                UIHelper.makeToast(getContext(), itemData.warnMsg);
                return;
            }
            setChecked(true);
            if (mPresenter != null) {
                mPresenter.onItemClick(itemData, param);
            }
        }
    }
}
